<template>
  <div class="login_container">
    <el-form>
      <el-row>
        <el-col :span="12" :xs="0"></el-col>
        <el-col :span="12" :xs="24">
          <el-form
            class="login_form"
            ref="loginForms"
            :model="loginForm"
            :rules="rules"
          >
            <h1>登录</h1>
            <!-- 登录名 -->
            <el-form-item prop="username">
              <el-input
                type="text"
                v-model="loginForm.username"
                placeholder="请输入用户名"
                :prefix-icon="User"
              ></el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item prop="password">
              <el-input
                type="password"
                v-model="loginForm.password"
                placeholder="请输入密码"
                :prefix-icon="Lock"
                show-password
              ></el-input>
            </el-form-item>
            <!-- 登录按钮 -->
            <el-form-item>
              <el-button
                type="primary"
                @click="user_login()"
                class="login_btn"
                :loading="loading"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script setup lang="ts">
//引入图标
import { User, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import useUserStore from '@/store/modules/user/user'
import { ElNotification } from 'element-plus'
import { getTimePeriod } from '@/utils/time'

let $router = useRouter()

let userStore = useUserStore()

//收集表单数据
import { reactive, ref } from 'vue'
let loginForm = reactive({
  username: 'admin',
  password: '111111 ',
})
let loading = ref(false)
let loginForms = ref()

//表单校验
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 19, message: '长度在 6 到 19 个字符', trigger: 'blur' },
  ],
}

//登录方法
const user_login = async () => {
  try {
    await loginForms.value.validate()
  } catch (err) {
    return
  }
  try {
    if (loading.value) {
      return
    }
    //  登录中
    loading.value = true
    await userStore.userLogin(loginForm)
    //登录成功
    $router.push('/')
    ElNotification({
      title: `Hi,${getTimePeriod()}好`,
      message: '登录成功～',
      type: 'success',
    })
    loading.value = false
  } catch (err) {
    //登录失败
    loading.value = false
    ElNotification({
      message: '登录失败,请检查用户名或密码',
      type: 'error',
    })
  }
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/login-bg.jpg') no-repeat;
  background-size: cover;

  .login_form {
    margin-left: 20%;
    padding: 40px;
    h1 {
      color: white;
      font-size: 20px;
      margin-bottom: 10px;
      text-align: center;
    }

    position: relative;
    width: 60%;
    margin-top: 35vh;
    background-color: transparent;
    transition: all 0.2s;

    &:hover {
      box-shadow: 0 0 20px #fff;
      border: 1px solid white;
      border-radius: 10px;
    }

    .login_btn {
      width: 100%;
      font-size: 16px;
    }
  }
}
</style>
